package com.ezgo.erc.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ezgo.erc.entity.Sysuser;
import com.ezgo.erc.master.exception.ServiceException;
import com.ezgo.erc.master.utils.Constants;
import com.ezgo.erc.master.utils.MD5Util;
import com.ezgo.erc.master.utils.Page;
import com.ezgo.erc.master.utils.StringUtil;
import com.ezgo.erc.service.SysuserService;
import com.ezgo.erc.utils.UserUtil;
import com.google.common.collect.Maps;

@Controller
@RequestMapping(value = "/sys/sysuser")
public class SysuserController extends BaseController {
	private Map<String, Object> map = Maps.newHashMap();
	private String tipmess = "";

	@Autowired
	private SysuserService sysuserService;

	@ModelAttribute
	public Sysuser get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return sysuserService.getSysuser(id);
		} else {
			return new Sysuser();
		}
	}

	@RequestMapping(value = "save")
	public String save(Sysuser user, Model model, RedirectAttributes redirectAttributes) {
		try {
			tipmess = user.getUserName() + "";
			map.clear();
			map.put("userName", user.getUserName());
			List<Sysuser> list = sysuserService.getSysuserList(map);
			user.setPassword(MD5Util.md5(user.getPassword()));
			if (list != null && list.size() > 0) {
				addMessage(model, String.format(Constants.PAGE_MESSAGE[0][3], tipmess, "hotelinfo " + tipmess + "已存在，无法添加"));
				return "/sys/sysuserForm";
			} else {
				boolean effectResult = sysuserService.saveSysuser(user);
				if (effectResult)
					addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[0][0], tipmess));
				else
					addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[0][1], tipmess));
			}
		} catch (ServiceException e) {
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[0][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "redirect:/sys/sysuser/?repage";
	}

	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		try {
			tipmess = get(id).getUserName() + "";
			boolean effectResult = sysuserService.deleteSysuser(id);
			if (effectResult)
				addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[1][0], tipmess));
			else
				addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[1][1], tipmess));
		} catch (ServiceException e) {
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[1][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "redirect:/sys/sysuser/?repage";
	}

	@RequestMapping(value = "update")
	public String update(Sysuser user, RedirectAttributes redirectAttributes) {
		try {
			tipmess = user.getUserName() + "";
			boolean effectResult = sysuserService.updateSysuser(user);
			if (effectResult)
				addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[2][0], tipmess));
			else
				addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[2][1], tipmess));
		} catch (ServiceException e) {
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[2][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "redirect:/sys/sysuser/?repage";
	}

	@RequestMapping(value = { "list", "" })
	public String list(Sysuser sysuser, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			map.clear();
			if (sysuser != null) {
				if (sysuser.getUserName() != null) {
					map.put("userName", sysuser.getId() + "");
				}
				if (sysuser.getPassword() != null) {
					map.put("password", sysuser.getPassword() + "");
				}
			}
			Page<Sysuser> page = sysuserService.getSysuserPage(new Page<Sysuser>(request, response), map);
			model.addAttribute("page", page);
		} catch (Exception e) {
			tipmess = sysuser.getUserName();
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[3][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
		}
		return "sys/sysuserList";
	}

	@RequestMapping(value = "view")
	public String view(Sysuser sysuser, RedirectAttributes redirectAttributes, Model model) {
		try {
			model.addAttribute("sysuser", sysuser);
		} catch (Exception e) {
			tipmess = sysuser.getUserName();
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[3][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
			return "redirect:/sys/sysuser/?repage";
		}
		return "sys/sysuserView";
	}

	@RequestMapping(value = "info")
	public String info(Model model) {
		model.addAttribute("sysuser", UserUtil.getUser());
		return "sys/sysuserInfo";
	}

	@RequestMapping(value = "modifyPwd")
	public String modifyPwd(String oldPassword, String newPassword, Model model) {
		Sysuser user = UserUtil.getUser();
		if (StringUtil.isNotBlank(oldPassword) && StringUtil.isNotBlank(newPassword)) {
			if (user.getPassword().equals(MD5Util.md5(oldPassword))) {
				map.clear();
				map.put("id", user.getId() + "");
				map.put("password", MD5Util.md5(newPassword));
				sysuserService.modifyUserPwd(map);
				addMessage(model, "修改密码成功");
			} else {
				addMessage(model, "修改密码失败，旧密码错误");
			}
		}
		model.addAttribute("sysuser", user);
		return "/sys/userModifyPwd";
	}

	@RequestMapping(value = "form")
	public String form(Sysuser sysuser, RedirectAttributes redirectAttributes, Model model) {
		try {
			model.addAttribute("sysuser", sysuser);
		} catch (Exception e) {
			tipmess = sysuser.getUserName();
			logger.error(String.format(Constants.EX_FORMAT_STR, this.getClass().getName(), e.getMessage()));
			addMessage(redirectAttributes, String.format(Constants.PAGE_MESSAGE[3][2], tipmess, StringUtil.abbr(e.getMessage(), 30)));
			return "redirect:/sys/sysuser/?repage";
		}
		return "sys/sysuserForm";
	}
}
